package com.hgyc.mom.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.hgyc.mom.system.entity.SysConfig;
import com.hgyc.mom.system.service.SysConfigService;
import com.hgyc.mom.system.vo.SysConfigParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 参数配置Controller
 *
 * @author fwj
 * @since 2025-05-13
 */
@Tag(name = "参数配置控制器", description = "参数配置控制器")
@RestController
@RequestMapping("/system/config")
public class SysConfigController {
    @Autowired
    private SysConfigService sysConfigService;

    @Operation(summary = "查询参数配置列表", description = "查询参数配置列表")
    @PostMapping("/pageList")
    public Result<IPage<SysConfig>> pageList(@Parameter(description = "查询参数") @RequestBody SysConfigParam sysConfigParam) {
        Page<SysConfig> page = new Page<>(sysConfigParam.getPage(), sysConfigParam.getPageSize());
        SysConfig sysConfig = new SysConfig();
        BeanUtils.copyProperties(sysConfigParam, sysConfig);
        QueryWrapper<SysConfig> wrapper = QueryWrapperUtil.build(sysConfig);
        IPage<SysConfig> list = sysConfigService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取参数配置详细信息", description = "获取参数配置详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "参数配置ID") @PathVariable("id") Long id) {
        return Result.success(sysConfigService.getById(id));
    }

    @Operation(summary = "新增参数配置", description = "新增参数配置")
    @PostMapping
    public Result save(@Parameter(description = "参数配置") @RequestBody SysConfig sysConfig) {
        sysConfigService.saveConfig(sysConfig);
        return Result.success();
    }

    @Operation(summary = "修改参数配置", description = "修改参数配置")
    @PutMapping
    public Result update(@Parameter(description = "参数配置") @RequestBody SysConfig sysConfig) {
        sysConfigService.updateConfig(sysConfig);
        return Result.success();
    }

    @Operation(summary = "删除参数配置", description = "删除参数配置")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(sysConfigService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 刷新参数缓存
     */
    @DeleteMapping("/refreshCache")
    public Result refreshCache()
    {
        sysConfigService.resetConfigCache();
        return Result.success();
    }
}